<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class users extends CI_Controller{
	
	public function get_info(){
		$session_username = $this->session->userdata('session_username');
		$session_role = $this->session->userdata('session_role');
		if(isset($session_username) && $session_username != '' && isset($session_role) && $session_role == '1'){
			$this->load->Model("users_DAO");
			$data = $this->users_DAO->get($_POST['username']);
			echo json_encode($data);
		} else {
			$this->logout();
		}
    }
	
	public function add(){
		$session_username = $this->session->userdata('session_username');
		$session_role = $this->session->userdata('session_role');
		if(isset($session_username) && $session_username != '' && isset($session_role) && $session_role == '1'){
			$this->load->Model("users_DAO");
			$this->load->Model("model/users_model");
			$users_model = new users_model();
			$users_model->username = $_POST['username'];
			hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');
			$users_model->password = hash('sha256', $_POST['password']);
			$users_model->email = $_POST['email'];
			$users_model->level = $_POST['level'];
			$data = $this->users_DAO->add($users_model);
			echo $data;
		} else {
			$this->logout();
		}
    }
	
	public function update(){
		$session_username = $this->session->userdata('session_username');
		$session_role = $this->session->userdata('session_role');
		if(isset($session_username) && $session_username != '' && isset($session_role) && $session_role == '1'){
			$this->load->Model("users_DAO");
			$changeData = $this->users_DAO->update($_POST['username'], $_POST['email'], $_POST['level']);
			if($_POST['password'] != ''){
				$changePassword = $this->users_DAO->changePassword($_POST['username'], hash('sha256', $_POST['password']));
			}
			echo $changeData ;
		} else {
			$this->logout();
		}
    }
	
	public function change_password(){
		$session_username = $this->session->userdata('session_username');
		$session_role = $this->session->userdata('session_role');
		if(isset($session_username) && $session_username != '' && isset($session_role) && $session_role == '1'){
			$this->load->Model("users_DAO");
			$oldpassword = hash('sha256', $_POST['oldpassword']);
			$password = hash('sha256', $_POST['password']);
			$data = $this->users_DAO->login($session_username, $oldpassword);
			if(sizeof($data) === 1){
				$changePassword = $this->users_DAO->changePassword($session_username, $password);
				echo $changePassword ;
			} else {
				echo '400';
			}
		} else {
			$this->logout();
		}
    }
	
	public function delete(){
		$session_username = $this->session->userdata('session_username');
		$session_role = $this->session->userdata('session_role');
		if(isset($session_username) && $session_username != '' && isset($session_role) && $session_role == '1'){
			$this->load->Model("users_DAO");
			$deleteData = $this->users_DAO->delete($_POST['username']);
			echo $deleteData;
		} else {
			$this->logout();
		}
    }
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
}